package io.ooad.banking.dd.domain;

import org.dayatang.domain.EntityRepository;
import org.dayatang.domain.InstanceFactory;

import javax.persistence.*;
import java.util.Date;
import java.util.List;

/**
 * 账户交易基类
 * Created by yyang on 16/9/16.
 */
@Entity
@Table(name = "transactions")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public abstract class AccountTransaction extends AbstractEntity {

    private BankAccount account;

    private Money amount;

    private Date transactionTime;

    AccountTransaction() {
    }

    public AccountTransaction(BankAccount account, Money amount, Date transactionTime) {
        this.account = account;
        this.amount = amount;
        this.transactionTime = transactionTime;
    }

    public BankAccount getAccount() {
        return account;
    }

    public Money getAmount() {
        return amount;
    }

    public Date getTransactionTime() {
        return transactionTime;
    }

    static List<AccountTransaction> findByAccount(BankAccount account) {
        return getRepository()
                .createCriteriaQuery(AccountTransaction.class)
                .eq("account", account)
                .asc("transactionTime")
                .list();
    }

    private static EntityRepository getRepository() {
        return InstanceFactory.getInstance(EntityRepository.class);
    }
}
